package state.component;

import state.Clip;
import state.Graphics;
import state.Manager;
import state.util.GameColor;

public class GameScrollBar extends Clip {
  private int bgCorlor = GameColor.WHITE_CODE;

  private int pointerColor = GameColor.RED_CODE;

  private int pointerHeight = 10;

  private Clip reuseClip = new Clip(0, 0, 0, 0);

  private float percent = 0;

  private int minTextY = Integer.MIN_VALUE;

  private int beginY = Integer.MAX_VALUE;

  private Clip view = null;

  private int lastY = Integer.MIN_VALUE;

  public GameScrollBar(int x, int y, int width, int height, Clip view1, Manager manager) {
    super(x, y, width, height);
    this.view = view1;
    manager.addScrollBar(this);
  }

  public void draw(Graphics g) {
    Clip.getClip1(g, reuseClip);
    clipRect(g);

    g.setColor(bgCorlor);
    g.fillRect(_x, _y, _width, _height);

    g.setColor(pointerColor);
    g.fillRect(_x, _y + (int) ((_height - pointerHeight) * percent / 100), _width, pointerHeight);

    reuseClip.applyClip(g);
  }

  public int getBgCorlor() {
    return bgCorlor;
  }

  public GameScrollBar setBgCorlor(int bgCorlor1) {
    this.bgCorlor = bgCorlor1;
    return this;
  }

  public int getPointerColor() {
    return pointerColor;
  }

  public GameScrollBar setPointerColor(int pointerColor1) {
    this.pointerColor = pointerColor1;
    return this;
  }

  public int getPointerHeight() {
    return pointerHeight;
  }

  public GameScrollBar setPointerHeight(int pointerHeight1) {
    this.pointerHeight = pointerHeight1;
    return this;
  }

  public float getPercent() {
    return percent;
  }

  public boolean canScrollDown() {
    return percent > 0;
  }

  public boolean canScrollUp() {
    return percent < 100;
  }

  public void setPercent(float percent1) {
    if (percent1 > 100) {
      percent1 = 100;
    } else if (percent1 < 0) {
      percent1 = 0;
    }
    this.percent = percent1;
  }

  public int getMinTextY() {
    return minTextY;
  }

  public int getBeginY() {
    return beginY;
  }

  public void setBeginY(int beginY1) {
    if (beginY1 < this.beginY) {
      this.beginY = beginY1;
    }
  }

  public int getLastY() {
    return lastY;
  }

  public void setLastY(int lastY1) {
    if (lastY1 > this.lastY) {
      this.lastY = lastY1;
    }
  }

  public Clip getView() {
    return view;
  }

  public void setView(Clip view1) {
    this.view = view1;
  }

  public void updateScroll(int inputBeginY, int inputLastY) {
    setBeginY(inputBeginY);
    setLastY(inputLastY);

    int allHeight = lastY - beginY;
    int heightToCalculateScroll = allHeight - view._height;
    if (heightToCalculateScroll < 0) {
      heightToCalculateScroll = 1;
    }
    int scrolledDistance = view._y - beginY;
    setPercent(scrolledDistance * 100.0f / heightToCalculateScroll);
    minTextY = view._y - heightToCalculateScroll;
  }

  public void reset() {
    beginY = Integer.MAX_VALUE;
    lastY = Integer.MIN_VALUE;
  }
}
